/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package util;

import dao.ManagerDAO;
import entity.Manager;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

/**
 *
 * @author DucND
 */
public class Emailer {

    public static String SendEmail(String email) {
        Properties props = new Properties();
        props.put("mail.smtp.host", "smtp.gmail.com");
        props.put("mail.smtp.socketFactory.port", "465");
        props.put("mail.smtp.socketFactory.class",
                "javax.net.ssl.SSLSocketFactory");
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.port", "465");

        Session session = Session.getDefaultInstance(props,
                new javax.mail.Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication("ducndse03296@fpt.edu.vn", "deonoidau");
            }
        });

        try {

            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress("ducndse03296@fpt.edu.vn"));
            message.setRecipients(Message.RecipientType.TO,
                    InternetAddress.parse(email));
            message.setSubject("Your password at Contract Management System");
            
            Manager m = ManagerDAO.getManagerInfoByEmail(email);
            message.setContent("Dear " + m.getUsername().trim() + ","
                    + "<br><br><i>** This is an automated message -- please do not reply as you will not receive a response. ** </i>"
                    + "<br><br>This message is in response to your request to get your account password."
                    + "<br>Your password is: <strong>" + m.getPassword().trim() + "</strong>"
                    + "<br><br>Student Management System", "text/html");
            Transport.send(message);
            return "Your password was sent to your email!";
        } catch (MessagingException e) {
            return "Error: " + e.getMessage();
        }
    }
}
